'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview mongodb创建连接池
 * @author sundbcn@gmail.com (sun debing)
 */

var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');

/*
 * Create mongodb connection pool.
 */
var createMongodbPool = function (mongodb_config) {
    var opts = {
        server: {
            auto_reconnect: true,
            socketOptions: {
                keepAlive: 1
            }
        },
        user: mongodb_config.user,
        pass: mongodb_config.password
    };
    var db = mongoose.createConnection(mongodb_config.host, mongodb_config.database, mongodb_config.port, opts);
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback() {});
    autoIncrement.initialize(db);
    return db;
};

exports.createMongodbPool = createMongodbPool;
